<?php
namespace app\merchant\controller;
use app\common\controller\AdminBase;
use app\common\model\Agent;
use app\common\model\Momerchant;
use app\common\model\WxOpen;
use app\common\model\Merchant;
use app\common\model\System;
use Alipay\Alipayobj;
use think\Controller;
use think\Request;
use think\Session;
use Wechat\Wxobjopen;
class Uploads extends AdminBase
{
    public function upload()
    {
        $file = request()->file('file');
        $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/card/');
        if($info){
            $imgurl['pic'] = '/uploads/card/'.$info->getSaveName();
            $imgurl['name'] = $info->getSaveName();
            $imgurl['ext'] = $info->getExtension();
            $res = $this->uploadwxcard($imgurl['pic']);
            if (isset($res['errcode'])){
                $data['code'] = 9003;
                $data['message'] = '上传失败！'.json_encode($res,JSON_UNESCAPED_UNICODE);
                return json($data);
            }else{
                $data['pic'] = $res['url'];
                $data['loc_pic']= $imgurl['pic'];
                $data['res']= json_encode($res,JSON_UNESCAPED_UNICODE);
                $data['code'] = 0;
                $data['message'] = '上传成功！';
                return json($data);
            }
        }else{
            $data['code'] = 9001;
            $data['message'] = $file->getError();
            return json($data);
        }
    }

    public function uploadserver()
    {
//        $param = request()->param();

        $file = request()->file('file');
//        return json($file);
        $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/shop/');
        if($info){
//            $imgurl['pic'] = '/uploads/shop/'.$info->getSaveName();
//            $imgurl['name'] = $info->getSaveName();
//            $imgurl['ext'] = $info->getExtension();
//            $res = $this->uploadwxcard($imgurl['pic']);
//            if ($res == false){
//                $data['code'] = 9003;
//                $data['message'] = '上传失败！';
//                return json($data);
//            }else{
                $data = '/uploads/shop/'.$info->getSaveName();
//                $data['loc_pic']= '/uploads/shop/'.$info->getSaveName();
//                $data['code'] = 0;
//                $data['message'] = '上传成功！';
                return $data;
//            }
        }else{
            $data['code'] = 9001;
            $data['message'] = $file->getError();
            return json($data);
        }
    }

    //阿里上传
    public function uploadali()
    {
        $file = request()->file('file');
        $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/card/');
        if($info){
            $imgurl['pic'] = '/uploads/card/'.$info->getSaveName();
            $imgurl['pic_ali'] = 'uploads/card/'.$info->getSaveName();
            $imgurl['name'] = $info->getSaveName();
            $imgurl['ext'] = $info->getExtension();
            $arr = request()->param();
//            return json($arr);
            $res = $this->upload_img_ali($imgurl['pic_ali'],$arr['mid']);
//            return json($res);
            if ($res['code'] == 0){
                $data['pic']= $imgurl['pic'];
                $data['pic_id'] = $res['data']['image_id'];
                $data['pic_url'] = $res['data']['image_url'];
                $data['code'] = 0;
                $data['message'] = '上传成功！';
                return json($data);
            }else{
//                $data['pic'] = $res['url'];
                $data['code'] = 9003;
                $data['message'] = '上传失败！';
                return json($data);
            }
        }else{
            $data['code'] = 9001;
            $data['message'] = $file->getError();
            return json($data);
        }
    }


    public function upload_img_ali($source_url,$mid)
    {
        $data['merchid'] = $mid;
        $User = new Merchant();
        $Agent = new Agent();
        $users = $User->where(array('id' => $data['merchid']))->find();
        $agent = $Agent->where(array('id' => $users['agent']))->find();
        $options = array(
            'appid' => $agent['ali_app_id'], // 填写高级调用功能的app id
            'rsaPrivateKey' => $agent['ali_app_key'],
        );
//        $source_url = "uploads/card/20180309/544840533a12238e1ca9dc3a3ba77c89.jpg";
        $info = pathinfo($source_url);
        $aliObj = new Alipayobj($options);
        $data2['app_id'] = $agent['ali_app_id'];
        $data2['method'] = "alipay.offline.material.image.upload";
        $data2['charset'] = "UTF-8";
        $data2['sign_type'] = "RSA2";
        $data2['timestamp'] = date('Y-m-d H:i:s');
        $data2['version'] = "1.0";
        $data2['app_auth_token'] = $users['ali_merid'];
        $data2['image_type'] = $info['extension'];
        $data2['image_name'] = $info['filename'];
        $data2['image_content'] = "@" . $source_url;
        $signContent = $aliObj->rsaSign($data2, $signType = "RSA2");
        $data2['sign'] = $signContent;
        $sj = $aliObj->upload_file($data2);
        $content = json_decode(iconv("GB2312", "UTF-8//IGNORE", $sj), true);
        $repson = $content['alipay_offline_material_image_upload_response'];
//        return $repson;
        if ($repson['code'] == "10000") {
            $msg['code'] = 0;
            $msg['data']['image_url'] = $repson['image_url'];
            $msg['data']['image_id'] = $repson['image_id'];
        } else {
            $msg['code'] = $repson['code'];
            $msg['message'] = $repson['msg'];
        }
        return $msg;
    }

    public function machuploads(){
        $file = request()->file('file');
        $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/mach/');
        if($info){
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
            $imgurl =$http_type.$_SERVER['HTTP_HOST'] .'/uploads/mach/'.$info->getSaveName();
            $data['pic'] =$imgurl;
            $data['code'] = 0;
            return json($data);
        }else{
            $data['code'] = 9001;
            $data['message'] = $file->getError();
            return json($data);
        }
    }

    //excel上传
    public function uploadexcel(){
        $file = request()->file('excel');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['ext'=>'xlsx,xls'])->move(ROOT_PATH . 'public' . DS . 'uploads/excel/');
        if ($info){
            $excel['url'] = '/uploads/excel/'. $info->getSaveName();
            $excel['code'] = 0;
            return json($excel);
        }else{
            $data['code'] = 9001;
            $data['message'] = $file->getError();
            return json($data);
        }
    }

    //微信上传
    public function uploadwxcard($img){
        $data = ["buffer" => '@.'.$img];
        $option = $this->option();
        $wxObj = new Wxobjopen($option);
        $res = $wxObj->uploadImg($data);
        return $res;
    }

    //微信API参数
    public function option(){
        Session::set('openid', null);
        $map['id'] = Session::get('login_top_mid');
        $System=new System();
        $WxOpen=new WxOpen();
        $Agent=new Agent();
        $Momerchant = new Momerchant();
        $momerchantinfo=$Momerchant->where($map)->field('wx_open_id,agent')->find();
        $wx_open_id = $momerchantinfo['wx_open_id'];
//        $config =$System->where(array('id' => 1))->find();
        $config =$Agent->where(array('id' => $momerchantinfo['agent']))->find();
        $users = $WxOpen->where('id',$wx_open_id)->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }
}
